﻿using Corina.Commands;
using Corina.Common;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;

namespace Corina.Cydee.Apphub.Viewmodels
{
    public class TransferViewmodel : ViewmodelBase
    {
        #region Constructor
        public TransferViewmodel()
        {
            DeviceClient.GetAllDeviceDataTransferTypeItemsCompleted += GetAllDeviceDataTransferTypeItemsCompleted;
            DeviceClient.InsertDeviceDataTransferTypeCompleted += InsertDeviceDataTransferTypeCompleted;
            DeviceClient.RemoveDeviceDataTransferTypeCompleted += RemoveDeviceDataTransferTypeCompleted;
            DeviceClient.UpdateDeviceDataTransferTypeCompleted += UpdateDeviceDataTransferTypeCompleted;
            DeviceClient.ChangeDeviceDataTransferTypeAvavailabilityCompleted+=ChangeDeviceDataTransferTypeAvavailabilityCompleted;
            if (DataContext.IsValid)
            {
                LoadData();
            }
        }

        
        #endregion

        #region Methods
        private void ChangeDeviceDataTransferTypeAvavailabilityCompleted(object sender, DataService.ChangeDeviceDataTransferTypeAvavailabilityCompletedEventArgs e)
        {
            LoadData();
        }

        private void UpdateDeviceDataTransferTypeCompleted(object sender, DataService.UpdateDeviceDataTransferTypeCompletedEventArgs e)
        {
            Operation = EntityOperation.None;
            UpdateEntity = new DeviceDataTransferTypeInfo();
            LoadData();
        }

        private void RemoveDeviceDataTransferTypeCompleted(object sender, DataService.RemoveDeviceDataTransferTypeCompletedEventArgs e)
        {
            LoadData();
        }

        private void InsertDeviceDataTransferTypeCompleted(object sender, DataService.InsertDeviceDataTransferTypeCompletedEventArgs e)
        {
            Operation = EntityOperation.None;
            UpdateEntity = new DeviceDataTransferTypeInfo();
            LoadData();
        }

        private void GetAllDeviceDataTransferTypeItemsCompleted(object sender, DataService.GetAllDeviceDataTransferTypeItemsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.Result == ResultValue.Succeed)
                {
                    ItemsCollection = new ObservableCollection<DeviceDataTransferTypeInfo>(e.Result.Entities.OfType<DeviceDataTransferTypeInfo>());
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
            IsBusy = false;
        }

        protected override void LoadData(params object[] paras)
        {
            IsBusy = true;
            DeviceClient.GetAllDeviceDataTransferTypeItemsAsync(Account, Password);
        }

        protected override void OperationChanged()
        {
            RaisePropertyChanged(() => ShowUpdate);
            RaisePropertyChanged(() => InsertCommand);
        }
        #endregion

        #region Collection
        private ObservableCollection<DeviceDataTransferTypeInfo> _itemsCollection;
        public ObservableCollection<DeviceDataTransferTypeInfo> ItemsCollection
        {
            get { return _itemsCollection; }
            set
            {
                _itemsCollection = value;
                RaisePropertyChanged(() => ItemsCollection);
            }
        }
        #endregion

        #region Update
        private DeviceDataTransferTypeInfo _updateEntity = new DeviceDataTransferTypeInfo();
        public DeviceDataTransferTypeInfo UpdateEntity
        {
            get { return _updateEntity; }
            set
            {
                _updateEntity = value;
                RaisePropertyChanged(() => UpdateEntity);
            }
        }
        #endregion

        #region Command
        public ICommand InsertCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return Operation == EntityOperation.None; },
                    ExecuteCallback = delegate
                    {
                        UpdateEntity = new DeviceDataTransferTypeInfo();
                        Operation = EntityOperation.Insert;
                    }
                };
            }
        }

        public ICommand UpdateCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);

                    UpdateEntity = ItemsCollection.FirstOrDefault(i => i.Id == id);
                    Operation = EntityOperation.Update;

                });
            }
        }

        public ICommand RemoveCommand
        {
            get
            {
                return new DelegateCommand(t =>
                    {
                        int id = Convert.ToInt32(t);

                        if (ShowConfirm(Language.LanguageKeys.IDS_DELETE_CONFIRM) == MessageBoxResult.OK)
                        {
                            DeviceClient.RemoveDeviceDataTransferTypeAsync(Account, Password, id);
                        }
                    });
            }
        }

        public ICommand ChangeCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);
                    bool flag = !ItemsCollection.FirstOrDefault(i => i.Id == id).IsAvailable;
                    DeviceClient.ChangeDeviceDataTransferTypeAvavailabilityAsync(Account, Password, id, flag);
                });
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate { 
                        switch(Operation)
                        {
                            case EntityOperation.Update:
                                DeviceClient.UpdateDeviceDataTransferTypeAsync(Account, Password, UpdateEntity.Title, UpdateEntity.Description, UpdateEntity.IsAvailable, UpdateEntity.Id);
                                break;
                            case EntityOperation.Insert:
                                DeviceClient.InsertDeviceDataTransferTypeAsync(Account, Password, UpdateEntity.Title, UpdateEntity.Description, UpdateEntity.IsAvailable);
                                break;
                        }
                    }
                };
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        UpdateEntity = new DeviceDataTransferTypeInfo();
                        Operation = EntityOperation.None;
                    }
                };
            }
        }
        #endregion
    }
}
